package com.ruipengkj.model;

import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TStockInOut;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.service.TEmployeeService;

public class TStockInOutModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	private Vector<List<Object>> rowdatas=new Vector<List<Object>>();
	private Vector<String> columns = new Vector<String>();
	
	private TEmployeeService tEmployeeService;
	
	public TStockInOutModel(){
		tEmployeeService = BuildSpringApplication.getInstance().getTEmployeeServiceImpl();
		columns.add("入/出库单编号");
		columns.add("仓库名称");
		columns.add("入/出");
		columns.add("出入原因类型");
		columns.add("操作人");
		columns.add("原因单据编号");
		columns.add("操作时间");
	}
	//获得行数
	public int getRowCount(){
		return rowdatas.size();
	}
	//获得列数
	public int getColumnCount(){
		return columns.size();
	}
	//获得单元格的值
	public Object getValueAt(int row,int column){
		
		return ((Vector)rowdatas.get(row)).get(column);
	}
	//获取列名
	public String getColumnName(int column){
		return (String)columns.get(column);
	}
	
	//获取列对应的类型
//	public Class getColumnClass(int column){
//		if(column==2){
//			return Integer.class;
//		}
//		return Object.class;
//	}
	//设置单元格是否可编辑
	public boolean isCellEditable(int rowIndex,int columnIndex){
		return false;
	}
	
	public void removeData(int row){
		rowdatas.remove(row);
	}
	
	
	
	//设置单元格的值
	public void setValueAt(Object aValue,int rowIndex,int columnIndex){
		//取出对应的记录
		Vector rowdata = (Vector)rowdatas.get(rowIndex);
		//删除原来的值
		rowdata.remove(columnIndex);
		//添加用户新修改的值
		rowdata.add(columnIndex,aValue);
	}

	public void setRowData(List<TStockInOut> tStockInOutList) {
		rowdatas.clear();
		if(tStockInOutList!=null&&tStockInOutList.size()!=0){
			for(int i=0;i<tStockInOutList.size();i++){
				TStockInOut tStockInOut=tStockInOutList.get(i);
				List<Object> row=new Vector<Object>();
				row.add(tStockInOut.getInOutID());//入/出库单编号
				if(tStockInOut.getTStore()!=null&&tStockInOut.getTStore().getName()!=null){
					row.add(tStockInOut.getTStore().getName());//仓库名称
				}else{
					row.add("");
				}
				if(tStockInOut.getType()==ApplicationContext.STOCK_TYPE_IN){
					row.add("入库");//入/出类型
				}else{
					row.add("出库");//入/出类型
				}
				row.add(ApplicationContext.STOCK_IN_OUT_SOURCE_TYPE_MAP().get(tStockInOut.getSourceType()));//出入原因类型
				
				try {
					if (tStockInOut.getOperator() != null) {
					TEmployee tEmployee = tEmployeeService.getNameByIdService(tStockInOut.getOperator());
					if (tEmployee != null && tEmployee.getName() != null)
						row.add(tEmployee.getName());
					else
						row.add("");
					} else 
						row.add("");
				} catch (Exception e) {
					e.printStackTrace();
				}
				row.add(tStockInOut.getSource());//原因单据编号
				row.add(tStockInOut.getHappenTime());//操作时间
				rowdatas.add(row);
			}
		}
	}
	
	public void clear(){
		rowdatas.clear();
	}
}
